#!/bin/bash
# 使用之前请务必在运行脚本的机器上安装sshpass和jq
# 使用JSON配置文件
CONFIG_FILE=$1

# 检查配置文件是否存在
if [ ! -f "$CONFIG_FILE" ]; then
    echo "错误：配置文件 $CONFIG_FILE 不存在"
    exit 1
fi

# 检查jq是否安装
if ! command -v jq &> /dev/null; then
    echo "错误：需要安装jq来解析JSON配置文件"
    echo "请运行：sudo apt-get install jq"
    exit 1
fi

# 统计节点总数
TOTAL_NODES=$(jq -r '.nodes | length' "$CONFIG_FILE")
echo "从配置文件中读取到 $TOTAL_NODES 个节点"

# 初始化统计变量
SUCCESS_COUNT=0
FAILED_COUNT=0
SUCCESS_NODES=()
FAILED_NODES=()

# 创建临时文件存储结果
TEMP_DIR="/tmp/link_test_$$"
mkdir -p "$TEMP_DIR"

echo ""
echo "开始在指定节点执行连接测试程序..."
# log_path=/apdcephfs_cq8/share_2906397/users/simonjjiang/codes/svlightweight/CosyVoice2_ft/examples/version2/continuousTTS/log_32B_RANK50GPU8_v2
#log_path=/apdcephfs_cq8/share_2906397/users/simonjjiang/codes/svlightweight/CosyVoice2_ft/examples/version2/continuousTTS/log_32B_RANK50GPU8_v2_yukai
# log_path=/apdcephfs_cq8/share_2906397/users/yukaiju/workspace/fuchiTTS/Tealab_TTS/examples/version2/continuousTTS
# mkdir -p ${log_path}
# LINE_NUM=0
# 读取JSON配置并执行

# export http_proxy="http://star-proxy.oa.com:3128"
# export https_proxy="http://star-proxy.oa.com:3128"
# export ftp_proxy="http://star-proxy.oa.com:3128"
# export no_proxy=".woa.com,mirrors.cloud.tencent.com,tlinux-mirror.tencent-cloud.com,tlinux-mirrorlist.tencent-cloud.com,localhost,127.0.0.1,mirrors-tlinux.tencentyun.com,.oa.com,.local,.3gqq.com,.7700.org,.ad.com,.ada_sixjoy.com,.addev.com,.app.local,.apps.local,.aurora.com,.autotest123.com,.bocaiwawa.com,.boss.com,.cdc.com,.cdn.com,.cds.com,.cf.com,.cjgc.local,.cm.com,.code.com,.datamine.com,.dvas.com,.dyndns.tv,.ecc.com,.expochart.cn,.expovideo.cn,.fms.com,.great.com,.hadoop.sec,.heme.com,.home.com,.hotbar.com,.ibg.com,.ied.com,.ieg.local,.ierd.com,.imd.com,.imoss.com,.isd.com,.isoso.com,.itil.com,.kao5.com,.kf.com,.kitty.com,.lpptp.com,.m.com,.matrix.cloud,.matrix.net,.mickey.com,.mig.local,.mqq.com,.oiweb.com,.okbuy.isddev.com,.oss.com,.otaworld.com,.paipaioa.com,.qqbrowser.local,.qqinternal.com,.qqwork.com,.rtpre.com,.sc.oa.com,.sec.com,.server.com,.service.com,.sjkxinternal.com,.sllwrnm5.cn,.sng.local,.soc.com,.t.km,.tcna.com,.teg.local,.tencentvoip.com,.tenpayoa.com,.test.air.tenpay.com,.tr.com,.tr_autotest123.com,.vpn.com,.wb.local,.webdev.com,.webdev2.com,.wizard.com,.wqq.com,.wsd.com,.sng.com,.music.lan,.mnet2.com,.tencentb2.com,.tmeoa.com,.pcg.com,www.wip3.adobe.com,www-mm.wip3.adobe.com,mirrors.tencent.com,csighub.tencentyun.com,9.223.97.217"
# wget http://jizhi.oa.com/taiji_client_golang/taiji_client -O /usr/bin/taiji_client && chmod +x /usr/bin/taiji_client
# rm /etc/yum.repos.d/ceph_el7_1.repo
# curl -o /etc/yum.repos.d/ceph_el7.repo http://gaia.repo.oa.com/ceph_el7.repo
# yum clean metadata
# yum -y remove ceph-fuse
# yum -y install ceph-fuse3
# yum -y install ceph-fuse
# yum install ceph-fuse  -y
#du -sh /apdcephfs_qy3/share_976139/users/xuelonggeng/code/wenet_tencent/examples/tencent_data/s0_fsq/tmp.py
# taiji_client mount -tk 98NKv5g6RsahR6AizN25dg -bf  TEALab_speech_task # /apdcephfs_qy3/share_976139
#du -sh /apdcephfs_zwfy/share_303841515/Tealab/data/translation/speech-to-text/29/parquet_000000060.tar
# taiji_client mount -tk 98NKv5g6RsahR6AizN25dg -bf TaiJi_HYAide_YuanBao_MM_ZW_H20  # /apdcephfs_zwfy/share_303841515/
#du -sh /mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/ckpt/fsq_from_epoch1_step7199_10W_multi_node/step_37499.pt
# taiji_client mount -tk 98NKv5g6RsahR6AizN25dg -bf TaiJi_HYAide_YuanBao_MM_ZW_H20 -l sg  jp_sgfd_cephfs:/mnt/apdcephfs_sgfd/share_303841515 #/mnt/apdcephfs_sgfd/share_303841515
# ln -s /apdcephfs_qy3 /teaspeech_ceph  


index=0
# INDEX=8
jq -r '.nodes[] | "\(.ip):\(.password)"' "$CONFIG_FILE" | while IFS=':' read -r NODE_IP PASSWORD; do
    echo $NODE_IP $PASSWORD
    [[ -z "$NODE_IP" ]] && continue
    echo "=== 开始连接节点 ${NODE_IP} ==="
    # 使用sshpass连接并执行ASR程序
    if sshpass -p "$PASSWORD" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no root@"$NODE_IP" << EOF
        echo "SSH连接成功！节点信息: "
        echo "主机名: \$(hostname)"
        echo "当前时间: \$(date)"
        echo "当前用户: \$(whoami)"
        echo "index:"$index
        taiji_client mount -tk 98NKv5g6RsahR6AizN25dg -bf TaiJi_HYAide_YuanBao_MM_ZW_H20 -l sg  jp_sgfd_cephfs:/mnt/apdcephfs_sgfd/share_303841515 
        ls /mnt/apdcephfs_sgfd/share_303841515
        echo "woshi gengxuelong"
        echo "节点 ${NODE_IP} 开始执行train程序..."
EOF
        index=$((index + 1))
    then
        echo "=== ✓ 已在节点 ${NODE_IP} 上成功启动ASR程序 ==="
        echo "$NODE_IP" >> "$TEMP_DIR/success_nodes.txt"
    else
        echo "=== ✗ 在节点 ${NODE_IP} 上启动ASR程序失败 ==="
        echo "$NODE_IP" >> "$TEMP_DIR/failed_nodes.txt"
    fi
done

# 统计结果
if [ -f "$TEMP_DIR/success_nodes.txt" ]; then
    SUCCESS_COUNT=$(wc -l < "$TEMP_DIR/success_nodes.txt")
    readarray -t SUCCESS_NODES < "$TEMP_DIR/success_nodes.txt"
else
    SUCCESS_COUNT=0
fi

if [ -f "$TEMP_DIR/failed_nodes.txt" ]; then
    FAILED_COUNT=$(wc -l < "$TEMP_DIR/failed_nodes.txt")
    readarray -t FAILED_NODES < "$TEMP_DIR/failed_nodes.txt"
else
    FAILED_COUNT=0
fi

# 打印统计结果
echo ""
echo "=========================================="
echo "连接测试完成统计："
echo "总节点数: $TOTAL_NODES"
echo "成功连接: $SUCCESS_COUNT 个节点"
echo "连接失败: $FAILED_COUNT 个节点"
echo "=========================================="

if [ $FAILED_COUNT -gt 0 ]; then
    echo "连接失败的节点IP："
    for ip in "${FAILED_NODES[@]}"; do
        echo "  ✗ $ip"
    done
fi

# 清理临时文件
rm -rf "$TEMP_DIR"

echo ""
echo "所有节点执行完毕 连接测试程序结束"